<template>
  <div>
    <div class="toolbar">
      <el-button type="primary" @click="showAddStudentDialog">新增学生</el-button>
      <div class="search">
        <el-input v-model="searchQuery" placeholder="请输入查询内容" />
        <el-button type="primary" @click="searchStudent">查询</el-button>
      </div>
    </div>
    <el-table :data="filteredData" border stripe>
      <el-table-column prop="id" label="学号" width="80" />
      <el-table-column prop="name" label="姓名" width="110" />
      <el-table-column prop="score" label="分数" width="100" />
      <el-table-column prop="access" label="评语" />
      <el-table-column fixed="right" label="Operations" min-width="120">
        <template #default="scope">
          <el-button link type="primary" size="large" @click="handleClick(scope.row)">
            Delete
          </el-button>
          <el-button link type="primary" size="large" @click="handleEdit(scope.row)">Edit</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 编辑模态框1 开始 -->
    <el-dialog v-model="dialogFormVisible1" title="编辑" width="500">
      <el-form :model="form">
        <el-form-item label="id" :label-width="formLabelWidth">
          <el-input v-model="form.id" disabled autocomplete="off" />
        </el-form-item>
        <el-form-item label="姓名" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="分数" :label-width="formLabelWidth">
          <el-input-number :min="0" :max="100" v-model="form.score" autocomplete="off" />
        </el-form-item>
        <el-form-item label="评语" :label-width="formLabelWidth">
          <el-input v-model="form.access" type="textarea" autocomplete="off" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogFormVisible1 = false">取消</el-button>
          <el-button type="primary" @click="editUser">
            确认
          </el-button>
        </div>
      </template>
    </el-dialog>
    <!-- 编辑模态框 结束 -->
    <!-- 添加模态框2 开始 -->
    <el-dialog v-model="dialogFormVisible2" title="添加学生" width="500">
      <el-form :model="newStudent">
        <el-form-item label="id" :label-width="formLabelWidth">
          <el-input v-model="newStudent.id" autocomplete="off" />
        </el-form-item>
        <el-form-item label="姓名" :label-width="formLabelWidth">
          <el-input v-model="newStudent.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="分数" :label-width="formLabelWidth">
          <el-input v-model="newStudent.score" autocomplete="off" />
        </el-form-item>
        <el-form-item label="评语" :label-width="formLabelWidth">
          <el-input v-model="newStudent.access" type="textarea" autocomplete="off" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogFormVisible2 = false">取消</el-button>
          <el-button type="primary" @click="addStudent">
            确认
          </el-button>
        </div>
      </template>
    </el-dialog>
    <!-- 添加模态框 结束 -->
  </div>
</template>

<script setup>
import { ref, computed } from 'vue';

const tableData = ref([
  {
    id: '001',
    name: '张三',
    score: 90,
    access: '很优秀',
  },
  {
    id: '002',
    name: '李四',
    score: 92,
    access: '优秀',
  },
  {
    id: '003',
    name: '王五',
    score: 83,
    access: '很努力，不错',
  },
  {
    id: '004',
    name: '赵六',
    score: 98,
    access: '666',
  },
]);

const form = ref({});
const searchQuery = ref('');
const filteredData = computed(() => {
  if (!searchQuery.value) {
    return tableData.value;
  }
  return tableData.value.filter(item =>
    Object.values(item).some(value =>
      String(value).toLowerCase().includes(searchQuery.value.toLowerCase())
    )
  );
});

const dialogFormVisible1 = ref(false);
const dialogFormVisible2 = ref(false);
const formLabelWidth = ref('120px');
const newStudent = ref({
  id: '',
  name: '',
  score: 0,
  access: '',
});

const showAddStudentDialog = () => {
  dialogFormVisible2.value = true;
};
const addStudent = () => {
  tableData.value.push({ ...newStudent.value });
  dialogFormVisible2.value = false;
};

const handleClick = (row) => {
  const index = tableData.value.indexOf(row);
  if (index > -1) {
    tableData.value.splice(index, 1);
  }
};

function handleEdit(row) {
  form.value = { ...row };
  dialogFormVisible1.value = true;
}
function editUser() {
  dialogFormVisible1.value = false;
}
</script>

<style>
.el-table {
  width: 100%;
  display: flex;
  text-align: center;
  justify-content: center;
  align-self: center;
}
.el-table.cell {
  text-align: center;
}
.toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}
.search {
  display: flex;
  align-items: center;
}
.search .el-input {
  margin-right: 10px;
  width: 250px;
}
#app {
  display: flex;
  justify-content: center;
  align-self: center;
}
</style>